package com.my.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 权限认证拦截器
 *
 */
public class AuthorityAnnotationInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

    if (handler instanceof HandlerMethod) {
        HandlerMethod hm = (HandlerMethod) handler;

        Class<?> clazz = hm.getBeanType();
        Method m = hm.getMethod();
        try {
            if (clazz != null && m != null) {
                boolean isClzAnnotation = clazz.isAnnotationPresent(Authority.class);
                boolean isMethondAnnotation = m.isAnnotationPresent(Authority.class);
                Authority authority = null;
                // 如果方法和类声明中同时存在这个注解，那么方法中的会覆盖类中的设定。
                if (isMethondAnnotation) {
                    authority = m.getAnnotation(Authority.class);
                } else if (isClzAnnotation) {
                    authority = clazz.getAnnotation(Authority.class);
                }
                System.out.println("==========="+authority.value());
                int code = -1;
                String msg = "";
                if (authority != null) {
                    if (AuthorityType.NoValidate == authority.value()) {System.out.println("==========="+authority.value());
                        // 标记为不验证,放行
                        return true ;
                    } else if (AuthorityType.NoAuthority == authority.value()) {System.out.println("==========="+authority.value());
                        // 不验证权限，验证是否登录
                        // TODO:
                        return true;
                    } else {
                        // 验证登录及权限
                        // TODO:

                        code = 1;
                        msg = "验证成功！";
                        return true;
                    }
                }

                // //跳转
                // String url = "";
                // response.getWriter().write("<script>top.location.href='"
                // + url + "'</script>");
                // return false;

                //return false;
            }
        } catch (Exception e) {
        }
    }
    return false;
    }   
}